package com.bocai.kelalacar.ui.car.set.act;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.kelalacar.App;
import com.bocai.kelalacar.R;
import com.bocai.kelalacar.base.BaseActivity;
import com.bocai.kelalacar.bean.ApplyWayBean;
import com.bocai.kelalacar.ui.car.main.act.MainActivity;
import com.bocai.kelalacar.ui.car.set.contract.WithdrawTypeContract;
import com.bocai.kelalacar.ui.car.set.module.WithdrawTypeModule;
import com.bocai.kelalacar.ui.car.set.presenter.WithdrawTypePresenter;
import com.bocai.kelalacar.util.DoubleUtil;
import com.bocai.kelalacar.util.SP;
import com.bocai.kelalacar.widget.WithdrawPopupInterface;
import com.bocai.kelalacar.widget.WithdrawPopupWindow;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by yuanfei on 2017/6/22.
 */

public class WithdrawActivity extends BaseActivity implements WithdrawTypeContract.view, WithdrawPopupInterface {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.right_btn)
    Button rightBtn;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.ll_add)
    LinearLayout llAdd;
    @BindView(R.id.ll_type)
    LinearLayout llType;
    @BindView(R.id.ed_money)
    EditText edMoney;
    @BindView(R.id.btn_next)
    Button btnNext;
    WithdrawPopupWindow popupWindow;
    List<String> list = new ArrayList<>();
    @Inject
    WithdrawTypePresenter presenter;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.tv_account)
    TextView tvAccount;
    @BindView(R.id.main)
    LinearLayout main;
    @BindView(R.id.img_type)
    ImageView imgType;
    String wayId;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_time)
    TextView tvTime;
    double money;
    private int index;

    @Override
    public int getLayoutId() {
        return R.layout.act_withdraw;
    }

    @Override
    public void initPresenter() {
        App.get(this).getAppComponent().addSub(new WithdrawTypeModule(this)).inject(this);
    }

    @Override
    public void initView() {
        initToolbar("我的钱包");
        initBack(imgBack,llBack);
        money = getIntent().getDoubleExtra("money",0);
        index = getIntent().getIntExtra("index",0);
        if (index == 2){
            tvMoney.setVisibility(View.GONE);
            edMoney.setText(money+"");
            edMoney.setEnabled(false);
        }
        tvMoney.setText("当前有 "+ DoubleUtil.m2(money)+"(元)可提现");
        for (int i = 0; i < 3; i++) {
            list.add(i + "");
        }
        tvType.setVisibility(View.GONE);
        presenter.applyWay();

        edMoney.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(wayId)&&!TextUtils.isEmpty(edMoney.getText().toString())){
                    btnNext.setEnabled(true);
                    btnNext.setTextColor(getResources().getColor(R.color.color_white));
                }else {
                    btnNext.setEnabled(false);
                    btnNext.setTextColor(getResources().getColor(R.color.text_color));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    @OnClick({R.id.ll_type, R.id.btn_next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_type:
                if (popupWindow!=null&&popupWindow.getBean()) {
                    backgroundAlpha(0.5f);
                    popupWindow.showAtLocation(findViewById(R.id.main), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                }else {
                    startActivityForResult(new Intent(WithdrawActivity.this,WithdrawTypeActivity.class),111);
                }
                break;
            case R.id.btn_next:
//                presenter.walletApply();
                if (TextUtils.isEmpty(wayId)){
                    showErrorTip("请选择提现方式");
                }else if (TextUtils.isEmpty(edMoney.getText().toString())){
                    showErrorTip("请输入提现金额");
                }else if (Double.parseDouble(edMoney.getText().toString())>50000){
                    showErrorTip("每笔提现最高限额5万元");
                }else {
                    if (index == 2){

                        presenter.walletApply(wayId,edMoney.getText().toString(),true);
                    }else {
                        presenter.walletApply(wayId, edMoney.getText().toString(), false);
                    }
                }
                break;
        }
    }

    @Override
    public void applyWaySuccess(ApplyWayBean bean) {
        tvTime.setText("3."+bean.getApplyMemo());
        popupWindow = new WithdrawPopupWindow(this, bean, this);
    }

    @Override
    public void addApplyWaySuccess() {

    }

    @Override
    public void deleteApplyWaySuccess(int index) {

    }

    @Override
    public void walletApplySuccess() {
        if (index == 2){
            SP.put(this,"PayBail",false);
        }
        showErrorTip("提现申请成功，48小时内到账");
        setResult(222);
        finish();
    }

    @Override
    public void showLoading(String title) {
        startProgressDialog(title);
    }

    @Override
    public void stopLoading() {
        stopProgressDialog();
    }

    @Override
    public void showErrorTip(String msg) {
        showShortToast(msg);
        if (msg.equals("您的登录已失效，请重新登录")){
            startActivity(new Intent(this,MainActivity.class).putExtra("index",1));
            finish();
        }
    }

    @Override
    public void getInformation(String type, String account,String wayId) {
        this.wayId = wayId;
        if (TextUtils.isEmpty(type)){
            tvType.setVisibility(View.GONE);
            Glide.with(this).load(R.mipmap.yinlian1).into(imgType);
        }else {
            if (type.equals("支付宝")){
                Glide.with(this).load(R.mipmap.zhifub).into(imgType);
            }else if (type.equals("微信")){
                Glide.with(this).load(R.mipmap.weichat).into(imgType);
            }
            tvType.setText(type);
            tvType.setVisibility(View.VISIBLE);
        }
        tvAccount.setText(account);
        popupWindow.dismiss();
        if (!TextUtils.isEmpty(wayId)&&!TextUtils.isEmpty(edMoney.getText().toString())){
            btnNext.setEnabled(true);
            btnNext.setTextColor(getResources().getColor(R.color.color_white));
        }else {
            btnNext.setEnabled(false);
            btnNext.setTextColor(getResources().getColor(R.color.text_color));
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 111){
            presenter.applyWay();
        }
    }
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        getWindow().setAttributes(lp);
    }
}
